package com.asa.appbarrecycleview

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.AbsListView
import android.widget.LinearLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.asa.appbarrecycleview.databinding.ActivityMainBinding
import com.squareup.picasso.Picasso

class MainActivity : AppCompatActivity() {
    private val binding: ActivityMainBinding by lazy { ActivityMainBinding.inflate(layoutInflater) }

    private var mCurrentPosition = 0
    private var mSuspensionBarHeight = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)


        binding.recycleView.layoutManager = LinearLayoutManager(this)
        binding.recycleView.adapter = FeedAdapter()

        binding.recycleView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
                mSuspensionBarHeight = binding.rlFeedContainer.height
            }

            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)

                val nextItemView = recyclerView.layoutManager?.findViewByPosition(mCurrentPosition+1)
                if (nextItemView != null) {
                    Log.e("日志", "onScrolled: top:${nextItemView.top}  barHeight:${mSuspensionBarHeight}")
                    if (nextItemView.top < mSuspensionBarHeight) {
                        binding.rlFeedContainer.y = (-(mSuspensionBarHeight - nextItemView.top)).toFloat()
                    } else {
                        binding.rlFeedContainer.y = 0f
                    }
                }

                if (recyclerView.layoutManager is LinearLayoutManager) {
                    val layoutManager = recyclerView.layoutManager as LinearLayoutManager
                    if (mCurrentPosition != layoutManager.findFirstVisibleItemPosition()) {
                        mCurrentPosition = layoutManager.findFirstVisibleItemPosition()
                        updateSuspensionor()
                    }
                }


            }
        })

    }

    /**
     * 更新状态栏显示数据
     */
    private fun updateSuspensionor() {
        Picasso.with(this)
            .load(getAvatarResId(mCurrentPosition))
            .centerInside()
            .fit()
            .into(binding.ivAvatar)
        binding.tvNickname.text = "NetEase $mCurrentPosition"
    }

    fun getAvatarResId(position:Int): Int {
        when (position % 4) {
            0 ->
                return R.drawable.avatar1;
            1 ->
                return R.drawable.avatar2;
            2 ->
                return R.drawable.avatar3;
            3 ->
                return R.drawable.avatar4;
        }
        return 0;
    }
}